<?php

namespace app\common\service\api;
use app\common\cache\api\VrabbitSdkCache;
use app\common\service\member\SettingService as MemberSetting;
use app\common\service\setting\Exception;
use app\common\service\system\SettingService;
use thirdsdk\Http;

/**
 * V兔接口
 */
class VrabbitSdkService
{
    const url = 'http://api.vv-tool.cn';
    /**
     * 初始化授权
     */
    private static function init()
    {
        $method = "/api/proxy/oauth/token";
        $result = array(
            'code'=>400,
            'msg'=>'系统繁忙，请稍后再试',
            'data'=>null,
        );
        //初始化配置信息
        $sysSetting = SettingService::info('vrabbit_account,vrabbit_accesss_key');
        if(!isset($sysSetting['vrabbit_account']) || !isset($sysSetting['vrabbit_accesss_key']) || !$sysSetting['vrabbit_account'] || !$sysSetting['vrabbit_accesss_key']){
            $result['msg'] = "请配置V兔接口参数";
            return $result;
        }
        $key = 'vtuToken';
        $authorization = VrabbitSdkCache::get($key);
        if (empty($authorization)) {
            $http = new Http('vrabbitInit');
            $body=[
                'grant_type'=>'client_credentials',
                'client_id'=>$sysSetting['vrabbit_account'],
                'client_secret'=>$sysSetting['vrabbit_accesss_key'],
            ];
            $api_res  = $http->post(self::url.$method,$body);
            if($api_res && isset($api_res['data']['access_token']) && isset($api_res['data']['token_type'])){
                $result['code'] =200;
                $result['msg'] ='授权成功';
                $result['data'] = $api_res['data']['token_type']." ".$api_res['data']['access_token'];
                $time = isset($api_res['data']['expires_in'])?$api_res['data']['expires_in']:172800;
                VrabbitSdkCache::set($key, $result['data'],($time/2));
                return $result;
            }
        }else{
            $result['code'] =200;
            $result['msg'] ='授权成功';
            $result['data'] = $authorization;
            return $result;
        }
        return $result;
    }

    /**
     * 旺旺验号
     * @Author: 易军辉
     * @DateTime:2024-08-07 18:12
     */
    public static function searchPlus($account='')
    {
        $method = "/tool/accounts/searchplus";
        $result = array(
            'code'=>400,
            'msg'=>'系统繁忙，请稍后再试',
            'data'=>null,
        );
        if(!$account){
            return $result;
        }
        $memberSetting = MemberSetting::info('is_account_register_verification');
        if($memberSetting['is_account_register_verification'] == 1){
            $init_data = self::init();
            if($init_data['code'] == 200){
                $authorization = $init_data['data'];
                $http = new Http('vrabbitSearchPlus');
                $headers = [
                    "Authorization:".$authorization,
                ];
                $api_res  = $http->get(self::url.$method."?account=".urlencode($account),$headers);
                if($api_res){
                    $result['code'] =200;
                    $result['msg'] ='查询成功';
                    $result['data'] = $api_res['data'];
                }
            }else{
                $result = $init_data;
            }
        }else{
            $result['code'] = 401;
            $result['msg'] = '未开启买号绑定时是否验号';
        }
        return $result;
    }
}
